#!encoding:utf-8


excludes={'：', '，', '；', '。', '！', '？', '\n'}

str_d = dict()
with open('zbkq.txt', 'r') as f:
    for i in f.read():
        if i not in excludes:
            str_d[i] = str_d.setdefault(i, 0) + 1

#print(sorted(str_d.items(), key= lambda x:x[1], reverse = True))
#import operator
#print(sorted(str_d.items(), key=operator.itemgetter(1), reverse = True))
#print(sorted(zip(str_d.values(), str_d.keys()), reverse = True))
new_d = list(sorted(str_d.items(), key = lambda x:x[1], reverse = True))
with open('char.csv', 'w', encoding='gb2312') as f:
    for c in new_d:
        f.write('{},{}\n'.format(c[0], c[1]))
